package com.boardour.ffmpegdemo.permission;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

public class XPermission {


    public static boolean hasPermissions(Context context, String... permissions) {

        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    public static void requestPermissions(Activity activity, int requestCode, String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity.requestPermissions(permissions, requestCode);
        }
    }

    public static void requestPermissions(Fragment fragment, int requestCode, String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            fragment.requestPermissions(permissions, requestCode);
        }
    }


    public static void onRequestPermissionsResult(int[] grantResults, XPermissionListener listener) {
        if (grantResults != null && PackageManager.PERMISSION_GRANTED == grantResults[0]) {
            if (listener != null) {
                listener.onSucceed();
            }
        }
    }


}
